<?php

namespace backend\controllers;

use backend\services\ArticleService;
use backend\services\CategoryService;
use Yii;
use backend\models\Article;
use backend\models\ArticleSearch;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * ArticleController implements the CRUD actions for Article model.
 */
class ArticleController extends AdminBaseController
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * action 之前就要执行的代码
     * @param \yii\base\Action $action
     * @return bool
     * @throws \yii\web\BadRequestHttpException
     */
    public function beforeAction($action)
    {
        $this->service = new ArticleService();
        $this->service->searchField = 'title';
        return parent::beforeAction($action); // TODO: Change the autogenerated stub
    }

    /**
     *获取页面变量
     */
    public function getViewVars($view, $info)
    {
        $viewVar = [];
        switch (strtolower($view)) {
            case 'edit':
                $categorys = (new CategoryService())->getListByLevel();
                foreach ($categorys as $key => $category) {
                    $categorys[$key]['html'] = str_repeat('&nbsp;', intval($category['level'])).'|-'.$category['name'];
                }
                $viewVar['category'] = $categorys;
                break;
        }
        return $viewVar;
    }


}
